<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

// 玩家 根据游戏角色  新增 流失用户统计 只与昨日对比
class Cdkey extends  Model{

	protected $table='game_cdkey';
    //protected $_db_select = 'mysql';

    public $timestamps = true;

    const DB_FIELDS = 'patch_id,area_list,items,expired_at,used_times,remark,title';
    
	public function paginate()
    {
    	$perPage = Request::get('per_page', 15);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $rw = \DB::connection($this->selectdb())->table($this->table)->orderBy("patch_id", 'desc')->skip($start)->take($perPage);
        self::SourceCondition($rw);
        $data = $rw->get();
        $data = $data->toArray();

        $total = \DB::connection($this->selectdb())->table($this->table)->get()->count();

        //$data = json_decode($data, true);
        // var_dump($data);
        // exit;

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
    public function getKeyName()
    {
        return "patch_id";
    }

    public function selectdb()
    {
        return 'mysql_bgac';
    }
    public function getConnectionName(){
        return $this->selectdb();
    }
    public function getAreaListAttribute($value)
    {
        return explode(',', $value);
    }

    public function setAreaListAttribute($value)
    {
        $this->attributes['area_list'] = implode(',', $value);
    }

    public static function SourceCondition(&$rw)
    {
        $cond = Request::all();
        if ($cond['title']){
            $rw->where("title", 'like', $cond["title"].'%');
        }
        if ($cond['area_list']){
            $rw->where("area_list", 'like', $cond['area_list']);
        }

        // if (isset($cond["expired_at"])){
        //     $send_time = $cond['expired_at'];
        //     (0 == $send_time) && $rw->where('expired_at', 0);
        // }
    }
    

    public static function assignByOut(&$one, $out){
        $flds = explode(',', self::DB_FIELDS);
        
 
        foreach ($flds as $fld) {
            isset($out[$fld]) && $one->{$fld} = $out[$fld];
        }

        if ($one->expired_at !== intval($one->expired_at))
        {
            $one->expired_at = strtotime($one->expired_at);
        }
    }

    // 批量 生产code
    public static function GenerateCode($cdkey_obj)
    {
        if (!is_object($cdkey_obj)){
            return -401;
        }

        $cur_count = \App\Models\CdkeyRef::GetCodeCount($cdkey_obj->patch_id);
        $count = $cdkey_obj->used_times - $cur_count;

        if (!$count) {
            return -201;
        }
        if ($count > 10000){
            $count = 10000;
        }

        for($i=0; $i< $count; $i++){
            $one = new \App\Models\CdkeyRef;
            $data = [
                'account' => 0,
                'player_id' => 0,
                'status'    =>0,
                'patch_id'  => $cdkey_obj->patch_id,
                'cdkey'     => uniqid() . mt_rand(10, 99),
            ];
            \App\Models\CdkeyRef::assignByOut($one, $data);
            $one->save();
        }
        return 0;
    }
}